﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TUIO;

namespace GRIP
{
    public class KinectNetworkAdapter : KinectAdapter, ITuioAdapter
    {
        public KinectNetworkAdapter(string Source)
        {
            this.Source = Source;
        }

        public void HandleNewFrame(ContactFrameReadyEventArgs frame)
        {
            List<TuioContact> NewContacts = frame.NewContacts;
            List<TuioContact> ExistingContacts = frame.ExistingContacts;
            List<TuioContact> RemovedContacts = frame.RemovedContacts;
            List<TuioContact> InactiveContacts = frame.InactiveContacts;

            for (int i = 0; i < NewContacts.Count; i++)
            {
                TuioContact remoteContact = NewContacts[i];
                TuioContact localContact = contacts[remoteContact.type];
                remoteContact.CopyTo(localContact);
                NewContacts[i] = localContact;
            }

            for (int i = 0; i < ExistingContacts.Count; i++)
            {
                TuioContact remoteContact = ExistingContacts[i];
                TuioContact localContact = contacts[remoteContact.type];
                remoteContact.CopyTo(localContact);
                ExistingContacts[i] = localContact;
            }

            for (int i = 0; i < RemovedContacts.Count; i++)
            {
                TuioContact remoteContact = RemovedContacts[i];
                TuioContact localContact = contacts[remoteContact.type];
                remoteContact.CopyTo(localContact);
                RemovedContacts[i] = localContact;
            }

            for (int i = 0; i < InactiveContacts.Count; i++)
            {
                TuioContact remoteContact = InactiveContacts[i];
                TuioContact localContact = contacts[remoteContact.type];
                remoteContact.CopyTo(localContact);
                InactiveContacts[i] = localContact;
            }
        }

        /// <summary>
        /// Returns the collection of each kinect contact, active or not.
        /// </summary>
        /// <returns>A read-only collection of all contacts</returns>
        public IEnumerable<TuioContact> GetContacts()
        {
            return contacts.Values;
        }
    }
}
